const Redis = require('ioredis');
const RedisLock = require('redlock');
const redisClient = new Redis({
    port: 6379, // Redis port
    host: '127.0.0.1', // Redis host
    password: '',
    db: 0,
})
const redLock = new RedisLock( // you should have one client for each independent redis node
    // or cluster
    [redisClient], {
        retryCount: 10,
        retryDelay: 200, // time in ms
        retryJitter: 200 // time in ms
    })

function sleep(time) {
    return new Promise((resolve) => {
        setTimeout(function () {
            resolve();
        }, time || 1000);
    });
}
let count = 5;
const key = 'count:5'

function fn() {
    for (let i = 0; i < 20; i++) {
        setTimeout(async () => {
            const lock = await redLock.lock(key, 2000)
            if (count == 0) {
                console.log('涩了')
            } else {
                console.log('zugou')
            }
            count--;
            console.log('现在的count', count);
            await lock.unlock()
        }, 0);

    }
}
fn()